package com.dehui.traders.ui.stockdetail.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.adapter.TableAdapter;
import com.dehui.traders.ui.stockdetail.bean.LabelTabel;
import com.dehui.traders.widgets.BaseViewHolder;
import com.dehui.traders.widgets.CommonAdapter;
import com.dehui.traders.widgets.MyHScrollView;

import java.util.List;

import me.goldze.mvvmhabit.utils.Utils;

public class StocksDetailSyAdapter extends CommonAdapter<LabelTabel> {

    private final int screenWidth;
    private final LayoutInflater inflater;
    private Activity activity;
    private TableAdapter.OnTableData onTableData;

    public StocksDetailSyAdapter(Context context, List<LabelTabel> mDatas,Activity activity) {
        super(context, mDatas, R.layout.list_item_table_sy);
        inflater = LayoutInflater.from(context);
        screenWidth = Utils.getScreen(context).widthPixels;
        this.activity = activity;
    }

    public void setOnTableData(TableAdapter.OnTableData onTableData){
        this.onTableData = onTableData;
    }

    @Override
    public void convert(BaseViewHolder helper, LabelTabel item, int position) {

        LinearLayout ll = helper.getView(R.id.ll_scroll);
        MyHScrollView scrollview = helper.getView(R.id.horizontal_scrollview);
        scrollview.setActivity(activity);

        onTableData.addScrollviews(scrollview);

        ll.removeAllViews();
        for (int i = 0; i < 5; i++){
            View view = inflater.inflate(R.layout.tab_head_detail_item, null);

            TextView tvItem0 = view.findViewById(R.id.tv_item0);
            LinearLayout.LayoutParams tvlp = (LinearLayout.LayoutParams) tvItem0.getLayoutParams();
            tvlp.width = screenWidth / 4;
            tvItem0.setLayoutParams(tvlp);

            TextView tvItem1 = view.findViewById(R.id.tv_item1);
            LinearLayout.LayoutParams tvlp1 = (LinearLayout.LayoutParams) tvItem1.getLayoutParams();
            tvlp1.width = screenWidth / 4;
            tvItem1.setLayoutParams(tvlp1);
            setContent(tvItem0,tvItem1,i,item);

            ll.addView(view);
        }

        TextView tvTime = helper.getView(R.id.tv_time);
        TextView tvStyle = helper.getView(R.id.tv_style);
        TextView tvSy = helper.getView(R.id.tv_tab_sy);
        tvTime.setText(item.getDate());
        tvStyle.setText("1".equals(item.getLabel()) ? "买入":"卖出");
        tvStyle.setTextColor("1".equals(item.getLabel()) ? ContextCompat.getColor(mContext,R.color.red) : ContextCompat.getColor(mContext,R.color.green));
        tvSy.setVisibility("1".equals(item.getLabel()) ? View.GONE:View.VISIBLE);


    }

    private void setContent(TextView tvItem0, TextView tvItem1, int i, LabelTabel item) {
        if (i == 0){
            tvItem0.setText(item.getAverage());
            tvItem1.setText(item.getDifferenceAveragePrice());
            if (!TextUtils.isEmpty(item.getDifferenceAveragePrice())){
                tvItem1.setTextColor(item.getDifferenceAveragePrice().startsWith("-") ? ContextCompat.getColor(mContext,R.color.green) : ContextCompat.getColor(mContext,R.color.red));
            }else{
                tvItem1.setVisibility(View.GONE);
            }
        }
        if (i == 1){
            tvItem0.setText(item.getOpen());
            tvItem1.setText(item.getDifferenceOpenPrice());
            if (!TextUtils.isEmpty(item.getDifferenceOpenPrice())){
                tvItem1.setTextColor(item.getDifferenceOpenPrice().startsWith("-") ? ContextCompat.getColor(mContext,R.color.green) : ContextCompat.getColor(mContext,R.color.red));
            }else{
                tvItem1.setVisibility(View.GONE);
            }
        }

        if (i == 2){
            tvItem0.setText(item.getHigh());
            tvItem1.setText(item.getDifferenceHighPrice());
            if (!TextUtils.isEmpty(item.getDifferenceHighPrice())){
                tvItem1.setTextColor(item.getDifferenceHighPrice().startsWith("-") ? ContextCompat.getColor(mContext,R.color.green) : ContextCompat.getColor(mContext,R.color.red));
            }else{
                tvItem1.setVisibility(View.GONE);
            }
        }

        if (i == 3){
            tvItem0.setText(item.getLow());
            tvItem1.setText(item.getDifferenceLowPrice());
            if (!TextUtils.isEmpty(item.getDifferenceLowPrice())){
                tvItem1.setTextColor(item.getDifferenceLowPrice().startsWith("-") ? ContextCompat.getColor(mContext,R.color.green) : ContextCompat.getColor(mContext,R.color.red));
            }else{
                tvItem1.setVisibility(View.GONE);
            }
        }

        if (i == 4){
            tvItem0.setText(item.getPrice());
            tvItem1.setText(item.getDifferencePrice());
            if (!TextUtils.isEmpty(item.getDifferencePrice())){
                tvItem1.setTextColor(item.getDifferencePrice().startsWith("-") ? ContextCompat.getColor(mContext,R.color.green) : ContextCompat.getColor(mContext,R.color.red));
            }else{
                tvItem1.setVisibility(View.GONE);
            }
        }


    }

}
